package com.survey.Controller;

import com.survey.pojo.data.QuestionBodyDO;
import com.survey.pojo.data.QuestionDO;
import com.survey.pojo.vo.QuestionBodyVO;
import com.survey.pojo.vo.QuestionVO;
import com.survey.pojo.vo.ResultVO;
import com.survey.service.QuestionBodyService;
import com.survey.service.QuestionService;
import com.survey.utils.DataUtils;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 邱思雨和王晓专用
 */
@Controller
@RequestMapping("/admin/question")
public class QuestionManageController {

    @Resource
    QuestionService questionService;

    @Resource
    QuestionBodyService questionBodyService;

    /**
     * 查看问卷上所有题目
     * sid=2
     */
    @RequestMapping("/questionManage")
    public String addQuestion(Integer pageCode,Integer sid, Model model) throws InvocationTargetException, IllegalAccessException {
        QuestionVO questionVO=new QuestionVO();
        questionVO.setFkSurvey(sid);
        questionVO.setPageSize(8);
        questionVO.setSize(5);
        questionVO.setPageCode(DataUtils.getPageCode(pageCode+""));
        PageBean<QuestionVO> pageBean = questionService.listQuestionBySurveyId(questionVO);
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("sid",sid);
        return "/admin/QuestionAdmin.jsp";
    }

    /**
     * 查看每一道题目信息
     */
    @RequestMapping("/search")
    public String search(Integer qid,Model model) throws Exception {
        //查询题目
        QuestionVO questionVO=new QuestionVO();
        questionVO.setId(qid);
        QuestionDO detailQuestion = questionService.findDetailQuestion(questionVO);
        //查询选项
        QuestionBodyVO questionBodyVO=new QuestionBodyVO();
        questionBodyVO.setId(detailQuestion.getFkBody());
        QuestionBodyDO detailQuestionBody = questionBodyService.findDetailQuestionBody(questionBodyVO);
        model.addAttribute("question",detailQuestion);
        model.addAttribute("answer",detailQuestionBody);
        return "/admin/QuestionEdit.jsp";
    }


    /**
     * 添加题目
     *var postData = "sid=" + $.trim($("#sid").val()) +
     *                 "&qsHead=" + $.trim($("#qsHead").val()) +
     *                 "&answer1=" + $.trim($("#answer1").val())+
     *                 "&answer2=" + $.trim($("#answer2").val())+
     *                 "&answer3=" + $.trim($("#answer3").val())+
     *                 "&answer4=" + $.trim($("#answer4").val())
     */
    @RequestMapping("/addQuestion")
    @ResponseBody
    public Result addQuestion(Integer sid,String qsHead,QuestionVO questionVO,
                    String answer1,String answer2,String answer3,String answer4
                    ) throws Exception {
        QuestionBodyVO questionBodyVO=new QuestionBodyVO();
            questionBodyVO.setOptiona(answer1);
            questionBodyVO.setOptionb(answer2);
            questionBodyVO.setOptionc(answer3);
            questionBodyVO.setOptiond(answer4);

        //先插body表
        Integer row = questionBodyService.addQuestionBody(questionBodyVO);

        if(row>0){
            questionVO.setFkSurvey(sid);
            questionVO.setHead(qsHead);
            questionVO.setFkBody(questionBodyVO.getId());
           return questionService.addQuestion(questionVO);
        }
        return null;
    }


    /**
     * 删除问题
     *   25 总体而言，你对网购是否满意？[单选题] 25 2
     *
     *   22	1	网购时，对于货物，你通常采用哪种邮递方式？[单选题]	22			2
     *   23	1	网购时，你对于货物送达能够接受的最长时间是？[单选题]	23			2
     * @param qid
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Integer qid) throws Exception {
        //System.out.println(qid);
        QuestionVO questionVO=new QuestionVO();
        questionVO.setId(qid);
       return questionService.deleteQuestion(questionVO);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(QuestionBodyVO questionBodyVO) throws Exception {
       // System.out.println(questionBodyVO);
       return questionBodyService.updateQuestionBody(questionBodyVO);
    }


}
